<template>
  <div class="mainor">
    <div class="main_spac1 paddingBottom5">
      <div class="main_white padding10 paddingBottom0 container">
        <!-- 头部 -->
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>确认提交</span>
          </div>
          <div class="main_crumbs_r boxnr"></div>
        </div>
        <!-- 主体表格 -->
        <div class="main_white">
          <div class="main_num boxdq padding10">
            <div class="main_num_l boxha">
              <p class="tip">
                请在（{{ info.open_time }}~{{ info.end_time }}）之间下单
              </p>
            </div>
            <div class="main_num_r box"></div>
          </div>
          <div class="main_table paddingBottom20" style="">
            <el-table
              ref="multipleTable"
              :data="info.list"
              element-loading-text="加载中"
              v-loading="listLoading"
              border
              highlight-current-row
              :header-cell-class-name="this.$verticaline"
            >
              <el-table-column
                label="商品图片"
                prop="name"
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  <div class="demo-image__error mainor_im">
                    <el-image
                      fit="fill"
                      :src="scope.row.pic"
                      :preview-src-list="scope.row.pics"
                    >
                      <div slot="error" class="image-slot">
                        <img :src="puzzleImg" />
                      </div>
                    </el-image>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="商品名称"
                prop="goods.name"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="单位"
                prop=""
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  {{
                    scope.row.spu.spu_unit ? scope.row.spu.spu_unit.title : ""
                  }}
                </template>
              </el-table-column>
              <el-table-column
                label="描述"
                prop="goods.describe"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="互调比"
                prop=""
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  {{ scope.row.spu ? scope.row.spu.package_num : "" }}
                </template>
              </el-table-column>
              <el-table-column
                label="库存量"
                prop=""
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  {{
                    scope.row.spu.inventory ? scope.row.spu.inventory.num : ""
                  }}
                </template>
              </el-table-column>
              <el-table-column
                label="批发数量"
                prop="num"
                :show-overflow-tooltip="true"
              ></el-table-column>
              <el-table-column
                label="批发单价"
                prop="market_price"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="批发金额"
                prop=""
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  {{ scope.row.money }}
                </template>
              </el-table-column>
              <el-table-column
                label="备注"
                prop="note"
                width="350"
                class-name="main_hei"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.note"
                    placeholder="输入备注..."
                    class="wid240"
                  ></el-input>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <div class="bottom_box paddingBottom20">
          <div class="bottom_left">
            <div class="item">
              <h3>收货人信息</h3>
              <p>姓名：{{ info.contact }}</p>
              <p>电话：{{ info.mobile }}</p>
              <p>地址：{{ info.address }}</p>
            </div>
            <div class="item">
              <h3>配送时间及备注</h3>
              <p>
                配送日期：
                <el-date-picker
                  v-model="form.sendDay"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                >
                </el-date-picker>
              </p>
              <p>
                配送时间：<el-dropdown @command="handleCommand">
                  <span class="el-dropdown-link">
                    {{
                      form.sendTime
                        ? info.send_time_list[form.sendTime]
                        : "不限"
                    }}<i class="el-icon-arrow-down el-icon--right"></i>
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="0">不限</el-dropdown-item>
                    <el-dropdown-item
                      v-for="(item, index) in info.send_time_list"
                      :key="index"
                      :command="index"
                      >{{ item }}</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </p>
              <p>
                备注：<input
                  style="width: 400px"
                  type="text"
                  placeholder="请输入"
                  v-model="form.note"
                />
              </p>
            </div>
          </div>
          <div class="bottom_right">
            <div class="top">
              <p>品种数量合计：{{ parseFloat(category_num) }}</p>
              <p>批发数量合计：{{ parseFloat(num) }}</p>
              <p>差额：{{ balance }} 元</p>
              <p>应付金额：{{ parseFloat(total) | twoPlace) }} 元</p>
              <p class="total">
                实付金额：<span class="price">
                  ￥{{ parseFloat(real_total) | twoPlace }}
                </span>
              </p>
            </div>
          </div>
        </div>
        <div :class="['commit_box']" id="commit_box">
          <div :class="['commit_box_child', { on: searchBarFixed }]">
            <div class="commit_box_left">
              <h3>支付方式</h3>
              <el-radio-group v-model="radio">
                <el-radio :label="3">银联</el-radio>
              </el-radio-group>
            </div>
            <div class="commit_box_right">
              <el-button type="primary" @click="commit">确认提交</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { puzzleImg } from "@/api/currency";
import { bulk_cart_close, bulk_cart_confirm } from "@/api/bulk/index";
import { parseTime, gettomorrow } from "@/utils/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import {
  balance_index,
  balance_settle
} from "@/api/financial/financialSalesman";
export default {
  computed: {
    ...mapGetters(["avatar"])
  },
  data() {
    return {
      puzzleImg,
      gettomorrow,
      parseTime,
      info: {
        address: "",
        mobile: "",
        contact: "",
        send_time_list: {},
        list: [],
        open_time: "",
        end_time: "",
        balance: ""
      },
      value2: "",
      total: 0, //应付
      real_total: 0, //实际付
      balance: 0,
      category_num: 0, //品种
      num: 0, //数量合计
      sendTime: "不限",
      listLoading: false,
      radio: 3,
      // 监听滚动
      searchBarFixed: false,
      form: {
        // sendDay: parseTime(gettomorrow()[0], "{y}-{m}-{d}"),
        sendDay: this.$route.params.send_time,
        sendTime: null,
        note: ""
      }
    };
  },
  created() {
    let cart_ids = this.$route.params.cart_ids;
    if (!cart_ids) {
      this.$router.back(-1);
    } else {
      this.bulk_cart_close({ cart_ids: cart_ids });
    }
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    // 监听滚动
    handleScroll() {
      // document.documentElement.clientHeight  浏览器窗口大小
      // scrollTop   滚动的距离
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("commit_box").offsetTop +
        document.getElementById("commit_box").offsetHeight
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    // 选择配送时间
    handleCommand(command) {
      if (command == 0) {
        this.form.sendTime = null;
      } else {
        this.form.sendTime = command;
      }
    },

    // 结算页面
    bulk_cart_close(form) {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        form
      );
      bulk_cart_close(data).then(res => {
        if (res.errcode == 0) {
          res.data.list.map(e => {
            e.money = this.$toFixed(
              parseFloat(e.num) * parseFloat(e.market_price)
            );
            this.total += parseFloat(e.money);
            this.num += parseFloat(e.num);
            if (e.goods.images.length > 0) {
              e.goods.images.map(a => {
                a.path = this.avatar.domain + "/" + a.path;
                if (a.main == 1) {
                  e.pic = a.path;
                }
              });
              e.pics = e.goods.images.map(s => s.path);
            } else {
              e.pic = "";
              e.pics = [];
            }
          });
          // 差额

          let balance = "";
          if (res.data.balance) {
            balance = res.data.balance;
          }
          let real_total = parseFloat(this.total) - parseFloat(balance);
          real_total > 0
            ? (this.real_total = real_total)
            : (this.real_total = 0);
          this.balance = balance;

          this.category_num = res.data.list.length;
          this.info = res.data;
          this.listLoading = false;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //   确认提交
    commit() {
      if (!this.form.sendDay) {
        this.$message.warning("请选择配送日期");
        return;
      }
      let list = this.info.list;
      let arr = [];
      list.map(e => {
        arr.push({
          id: e.id,
          note: e.note
        });
      });

      let form = {
        send_time: this.form.sendDay,
        send_time_id: this.form.sendTime ? this.form.sendTime : 0,
        remark: this.form.note,
        cart_ids: JSON.stringify(arr)
      };
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        form
      );

      bulk_cart_confirm(data).then(res => {
        if (res.errcode == 0) {
          this.$router.push({
            name: "bulk_shopcart_pay",
            params: { oid: res.data.oid }
          });
        } else {
          this.$message.warning(res.errmsg);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.tip {
  color: #ea1212;
}
// .main_table {
//   min-height: 400px;
// }
.main_table /deep/.el-table--small td .cell {
  height: 50px;
  line-height: 50px;
}
.use_advice {
  padding: 0 10px;
  &:first-child {
    color: #03a98e;
  }
  &:last-child {
    color: #ea1212;
  }
}

// 底部信息区域
.bottom_box {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  .item {
    margin-bottom: 15px;
    h3 {
      color: #333333;
      font-weight: 700;
      font-size: 14px;
      line-height: 2;
    }
    p {
      color: #333333;
      font-size: 14px;
      line-height: 1.5;
    }
  }
  .bottom_right {
    text-align: right;
    .top {
      p {
        line-height: 1.5;
        color: #333333;
      }
      .total {
        display: flex;
        align-content: center;
        align-items: center;
      }
      .price {
        color: #ea1212;
        font-size: 20px;
        font-weight: 700;
        padding-right: 6px;
      }
    }
    .bottom {
      margin-top: 27px;
      p {
        color: #ea1212;
        margin-bottom: 27px;
      }
      .el-button {
        width: 120px;
      }
    }
  }
}
.commit_box {
  position: relative;
  border-top: 1px solid #e3e3e5;
  padding-top: 20px;
  padding-bottom: 20px;
  width: 100%;
  .commit_box_child {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .commit_box_left {
    h3 {
      color: #333333;
      font-weight: 700;
      font-size: 14px;
      line-height: 2;
    }
  }
}
.container {
  position: relative;
}
.on {
  width: calc(100% - 212px);
  padding-left: 0;
  position: fixed;
  bottom: 0;
  background-color: #ffffff;
  z-index: 9999;
  padding-top: 20px;
  background-color: #fefeff;
}

/deep/.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 160px;
}
/deep/.el-input__inner {
  border: none;
}
</style>
